package com.wy.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wy.springbootblog.mapper.CategoryMapper;
import com.wy.springbootblog.pojo.Article;
import com.wy.springbootblog.pojo.Category;
import com.wy.springbootblog.pojo.Result;
import com.wy.springbootblog.pojo.SearchBean;
import com.wy.springbootblog.service.CategoryService;
import com.wy.springbootblog.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author work
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    // 新增文章分类
    @Override
    public Result<Void> saveCategory(Category category) {
        // 1.判断用户是否有操作权限
        Map<String,Object> map = ThreadLocalUtil.get();
        String role = (String) map.get("role");
        if (!Objects.equals(role, "admin")) {
            // 没有新增文章分类权限
            return Result.error("该用户无新增文章分类权限");
        }
        // 2.查询文章分类是否已经存在
        boolean bool = categoryMapper.exists(new LambdaQueryWrapper<Category>().eq(Category::getName,category.getName()));
        if (bool) {
            return Result.error("文章分类已存在");
        }
        // 3.新增文章分类
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.insert(category);
        return Result.success();
    }

    @Override
    public Result<Void> deleteCategory(Integer id) {
        // 1.判断用户是否有操作权限
        Map<String,Object> map = ThreadLocalUtil.get();
        String role = (String) map.get("role");
        if (!Objects.equals(role, "admin")) {
            // 没有新增文章分类权限
            return Result.error("该用户无删除文章分类权限");
        }
        // 2.查询文章分类是否已经存在
        boolean bool = categoryMapper.exists(new LambdaQueryWrapper<Category>().eq(Category::getId,id));
        if (!bool) {
            return Result.error("文章分类不存在");
        }
        // 3.删除文章分类
        categoryMapper.deleteById(id);
        return Result.success();
    }

    // 分页获取文章分类
    @Override
    public Result<List<Category>> categoryList(SearchBean<Category> searchBean) {
        // 分页查询
        Page<Category> page = new Page<Category>(searchBean.getPageNum(), searchBean.getPageSize());
        QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
        queryWrapper.eq("flag", 0);
        // 模糊查询文章分类name
        if (searchBean.getQuery() != null && searchBean.getQuery().getName() != null) {
            queryWrapper.like("name", searchBean.getQuery().getName());
        }
        categoryMapper.selectPage(page, queryWrapper);
        return Result.success(page.getRecords(), page.getTotal());
    }

    // 修改文章分类
    @Override
    public Result<Void> updateCategory(Category category) {
        // 1.判断用户身份权限
        Map<String, Object> map = ThreadLocalUtil.get();
        String role = (String) map.get("role");
        if (!Objects.equals(role, "admin")) {
            // 没有修改分类权限
            return Result.error("该用户无修改文章分类权限");
        }
        // 2.判断文章分类是否存在
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();
        queryWrapper.eq(Category::getId, category.getId());
        queryWrapper.eq(Category::getFlag, 0);
        Category categoryQuery = categoryMapper.selectOne(queryWrapper);
        if (categoryQuery == null) {
            return Result.error("文章分类不存在");
        }
        // 3.更新文章分类
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateById(category);
        return Result.success();
    }
}
